﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MAT.Model;

namespace MAT.Controllers
{
    class ctrManageSupplier
    {
        private frmManageSupplier _frmManageSupplier = null;

        public frmManageSupplier FrmManageSupplier
        {
            get { return _frmManageSupplier; }
            set { _frmManageSupplier = value; }
        }

        MAT_Context context = MATContext.getContext();
        List<SUPPLIER> sup = null;

        private string getID()
        {
            sup = context.SUPPLIERs.ToList();
            string a = sup[sup.Count - 1].SUPPLIER_ID.ToString();
            int dem = Convert.ToInt16(a.Substring(a.Length - 3));
            if (dem >= 99)
                a = "" + ++dem;
            else
                if (dem >= 9)
                    a = "0" + ++dem;
                else
                    a = "00" + ++dem;
            return "SU" + a;
        }

        private bool check()
        {
            if (_frmManageSupplier.txtSupplierName.Text == "")
            {
                MessageBox.Show("Name is empty");
                _frmManageSupplier.txtSupplierName.Focus();
                return false;
            }
            if (_frmManageSupplier.txtSupplierAddress.Text == "")
            {
                MessageBox.Show("Address is empty");
                _frmManageSupplier.txtSupplierAddress.Focus();
                return false;
            }
            if (_frmManageSupplier.txtSuplierPhone.Text == "")
            {
                MessageBox.Show("Phone is empty");
                _frmManageSupplier.txtSuplierPhone.Focus();
                return false;
            }
            else
                return true;
        }

        private void load_combobox()
        {
            _frmManageSupplier.cmbSupplierName.DataSource = context.SUPPLIERs.ToList();
            _frmManageSupplier.cmbSupplierName.DisplayMember = "SUPPLIER_NAME";
            _frmManageSupplier.cmbSupplierName.ValueMember = "SUPPLIER_ID";
        }

        public void frmManageSupplier_Load()
        {
            _frmManageSupplier.txtSupplierName.GotFocus += new EventHandler(txtSupplierName_Focus);
            _frmManageSupplier.txtSupplierName.MouseClick += new MouseEventHandler(txtSupplierName_Focus);
            _frmManageSupplier.txtSupplierAddress.GotFocus += new EventHandler(txtSupplierAddress_Focus);
            _frmManageSupplier.txtSupplierAddress.MouseClick += new MouseEventHandler(txtSupplierAddress_Focus);
            _frmManageSupplier.txtSuplierPhone.GotFocus += new EventHandler(txtSupplierPhone_Focus);
            _frmManageSupplier.txtSuplierPhone.MouseClick += new MouseEventHandler(txtSupplierPhone_Focus);
            _frmManageSupplier.txtSupplierFax.GotFocus += new EventHandler(txtSupplierFax_Focus);
            _frmManageSupplier.txtSupplierFax.MouseClick += new MouseEventHandler(txtSupplierFax_Focus);
            _frmManageSupplier.txtSupplierEmail.GotFocus += new EventHandler(txtSupplierEmail_Focus);
            _frmManageSupplier.txtSupplierEmail.MouseClick += new MouseEventHandler(txtSupplierEmail_Focus);
            load_combobox();
        }

        public void btnAdd_Click()
        {
            if (_frmManageSupplier.btnAdd.Text == "Add")
            {
                if (check())
                {
                    if (context.SUPPLIERs.FirstOrDefault(sup => sup.SUPPLIER_NAME == _frmManageSupplier.txtSupplierName.Text) == null)
                    {
                        SUPPLIER supplier = new SUPPLIER();
                        supplier.SUPPLIER_ID = _frmManageSupplier.txtSupplierID.Text;
                        supplier.SUPPLIER_NAME = _frmManageSupplier.txtSupplierName.Text;
                        supplier.SUPPLIER_ADDRESS = _frmManageSupplier.txtSupplierAddress.Text;
                        supplier.PHONE_NUM = _frmManageSupplier.txtSuplierPhone.Text;
                        supplier.SUP_EMAIL = _frmManageSupplier.txtSupplierEmail.Text;
                        supplier.FAX = _frmManageSupplier.txtSupplierFax.Text;
                        try
                        {
                            context.SUPPLIERs.AddObject(supplier);
                            context.SaveChanges();
                            MessageBox.Show("SUCCESSFUL!");
                            load_combobox();
                        }
                        catch { }
                        _frmManageSupplier.btnDelete.Enabled = true;
                        _frmManageSupplier.btnEdit.Enabled = true;
                        _frmManageSupplier.cmbSupplierName.Enabled = true;
                        _frmManageSupplier.txtSupplierID.Text = "";
                        _frmManageSupplier.txtSupplierName.Text = "";
                        _frmManageSupplier.txtSupplierAddress.Text = "";
                        _frmManageSupplier.txtSuplierPhone.Text = "";
                        _frmManageSupplier.txtSupplierEmail.Text = "";
                        _frmManageSupplier.txtSupplierFax.Text = "";
                        _frmManageSupplier.btnReset.Visible = false;
                        _frmManageSupplier.btnAdd.Text = "New";
                        _frmManageSupplier.btnClose.Text = "Close";
                    }
                    else
                        MessageBox.Show("Supplier is exist!");
                }
                return;
            }
            if (_frmManageSupplier.btnAdd.Text == "New")
            {
                _frmManageSupplier.btnAdd.Text = "Add";
                _frmManageSupplier.btnDelete.Enabled = false;
                _frmManageSupplier.btnEdit.Enabled = false;
                _frmManageSupplier.cmbSupplierName.Enabled = false;
                _frmManageSupplier.txtSupplierID.Text = "";
                _frmManageSupplier.txtSupplierName.Text = "";
                _frmManageSupplier.txtSupplierAddress.Text = "";
                _frmManageSupplier.txtSuplierPhone.Text = "";
                _frmManageSupplier.txtSupplierEmail.Text = "";
                _frmManageSupplier.txtSupplierFax.Text = "";
                _frmManageSupplier.txtSupplierID.Text = getID();
                _frmManageSupplier.btnClose.Text = "Cancel";
                _frmManageSupplier.btnReset.Visible = true;
                return;
            }
        }

        public void btnReset_Click()
        {
            _frmManageSupplier.txtSupplierName.Text = "";
            _frmManageSupplier.txtSupplierAddress.Text = "";
            _frmManageSupplier.txtSuplierPhone.Text = "";
            _frmManageSupplier.txtSupplierEmail.Text = "";
            _frmManageSupplier.txtSupplierFax.Text = "";
        }

        public void btnClose_Click()
        {
            if (_frmManageSupplier.btnClose.Text == "Close")
            {
                _frmManageSupplier.Close();
                MAT.Controllers.ctrMain._frmMain.pnlMain.Visible = false;
            }
            if (_frmManageSupplier.btnClose.Text == "Cancel")
            {
                _frmManageSupplier.btnClose.Text = "Close";
                _frmManageSupplier.btnDelete.Enabled = true;
                _frmManageSupplier.btnEdit.Enabled = true;
                _frmManageSupplier.cmbSupplierName.Enabled = true;
                _frmManageSupplier.txtSupplierID.Text = "";
                _frmManageSupplier.txtSupplierName.Text = "";
                _frmManageSupplier.txtSupplierAddress.Text = "";
                _frmManageSupplier.txtSuplierPhone.Text = "";
                _frmManageSupplier.txtSupplierEmail.Text = "";
                _frmManageSupplier.txtSupplierFax.Text = "";
                _frmManageSupplier.btnReset.Visible = false;
                _frmManageSupplier.btnAdd.Text = "New";
                return;
            }
        }

        public void btnDelete_Click()
        {
            SUPPLIER supplier = context.SUPPLIERs.FirstOrDefault(sup => sup.SUPPLIER_ID == _frmManageSupplier.txtSupplierID.Text);
            if (supplier != null)
            {
                try
                {
                    context.SUPPLIERs.DeleteObject(supplier);
                    context.SaveChanges();
                    MessageBox.Show("SUCCESSFUL!");
                    load_combobox();
                }
                catch { }
            }
            else
                MessageBox.Show("Not Found!");
            return;
        }

        public void btnEdit_Click()
        {
            SUPPLIER supplier = context.SUPPLIERs.FirstOrDefault(sup => sup.SUPPLIER_ID.Trim() == _frmManageSupplier.txtSupplierID.Text.Trim());
            if (supplier != null)
            {
                if (context.SUPPLIERs.FirstOrDefault(sup => sup.SUPPLIER_NAME.ToLower() == _frmManageSupplier.txtSupplierName.Text.ToLower()) == null)
                {
                    try
                    {
                        supplier.SUPPLIER_NAME = _frmManageSupplier.txtSupplierName.Text;
                        supplier.SUPPLIER_ADDRESS = _frmManageSupplier.txtSupplierAddress.Text;
                        supplier.PHONE_NUM = _frmManageSupplier.txtSuplierPhone.Text;
                        supplier.SUP_EMAIL = _frmManageSupplier.txtSupplierEmail.Text;
                        supplier.FAX = _frmManageSupplier.txtSupplierFax.Text;
                        context.SaveChanges();
                        MessageBox.Show("SUCCESSFUL!");
                        load_combobox();
                    }
                    catch { }
                }
                else
                    MessageBox.Show("Supplier name is exist!");
            }
            else
                MessageBox.Show("Not Found!");
            return;
        }

        public void cmbSupplierName_SelectedIndexChanged()
        {
            try
            {
                string a = _frmManageSupplier.cmbSupplierName.SelectedValue.ToString();
                SUPPLIER supplier = context.SUPPLIERs.FirstOrDefault(sup => sup.SUPPLIER_ID == a);
                if (supplier != null)
                {
                    _frmManageSupplier.txtSupplierID.Text = supplier.SUPPLIER_ID;
                    _frmManageSupplier.txtSupplierName.Text = supplier.SUPPLIER_NAME;
                    _frmManageSupplier.txtSupplierAddress.Text = supplier.SUPPLIER_ADDRESS;
                    _frmManageSupplier.txtSuplierPhone.Text = supplier.PHONE_NUM;
                    _frmManageSupplier.txtSupplierEmail.Text = supplier.SUP_EMAIL;
                    _frmManageSupplier.txtSupplierFax.Text = supplier.FAX;
                }
            }
            catch { }
        }

        public void txt_KeyDown(KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                btnAdd_Click();
        }

        //Select all data in textboxes

        void txtSupplierName_Focus(object sender, EventArgs e)
        {
            _frmManageSupplier.txtSupplierName.SelectAll();
        }

        void txtSupplierAddress_Focus(object sender, EventArgs e)
        {
            _frmManageSupplier.txtSupplierAddress.SelectAll();
        }

        void txtSupplierPhone_Focus(object sender, EventArgs e)
        {
            _frmManageSupplier.txtSuplierPhone.SelectAll();
        }

        void txtSupplierFax_Focus(object sender, EventArgs e)
        {
            _frmManageSupplier.txtSupplierFax.SelectAll();
        }

        void txtSupplierEmail_Focus(object sender, EventArgs e)
        {
            _frmManageSupplier.txtSupplierEmail.SelectAll();
        }
    }
}
